package com.example.demo.controller;

import com.example.demo.model.ProductionTracking;
import com.example.demo.service.ProductionTrackingService;
import com.example.demo.util.ErrorResponse;
import com.example.demo.util.ResponseUtil;
import com.example.demo.util.PageResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

import java.sql.Date;
import java.sql.Timestamp;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/productionTracking")
public class ProductionTrackingController {
    private final ProductionTrackingService productionTrackingService;

    public ProductionTrackingController(ProductionTrackingService productionTrackingService) {
        this.productionTrackingService = productionTrackingService;
    }

    @PostMapping
    public ResponseEntity<?> addProductionTracking(@RequestBody ProductionTracking productionTracking) {
        try {
            productionTrackingService.addProductionTracking(productionTracking);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.CONFLICT)
                    .body(new ErrorResponse(HttpStatus.CONFLICT.value(), e.getMessage()));
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateProductionTracking(
            @PathVariable int id,
            @RequestBody ProductionTracking productionTracking) {
        if (id != productionTracking.getId()) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        boolean isUpdated = productionTrackingService.updateProductionTracking(productionTracking) > 0;
        return isUpdated
                ? new ResponseEntity<>(HttpStatus.OK)
                : new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @GetMapping
    public ResponseEntity<List<ProductionTracking>> getAllProductionTracking() {
        List<ProductionTracking> productionTrackings = productionTrackingService.getAllProductionTracking();
        return new ResponseEntity<>(productionTrackings, HttpStatus.OK);
    }

    @GetMapping("/searchProductionTracking")
    public ResponseEntity<PageResponse<ProductionTracking>> searchProductionTracking(
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) Integer managerId,
            @RequestParam(required = false) String auditStatus,
            @RequestParam(required = false) String batchNumber,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        try {
            if (page <= 0) {
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }
            Pageable pageable = PageRequest.of(page - 1, size);
            Page<ProductionTracking> result = productionTrackingService.searchProductionTracking(productName, department, managerId, auditStatus, batchNumber, pageable);
            return ResponseEntity.ok(ResponseUtil.success(result.getTotalElements(), result.getContent()));
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<ProductionTracking> getProductionTrackingById(@PathVariable int id) {
        ProductionTracking productionTracking = productionTrackingService.getProductionTrackingById(id);
        return productionTracking != null
                ? new ResponseEntity<>(productionTracking, HttpStatus.OK)
                : new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProductionTracking(@PathVariable int id) {
        productionTrackingService.deleteProductionTracking(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 获取审核通过的信息数与总信息数的比例
     * @return 审核通过的信息数与总信息数的比例
     */
    @GetMapping("/approvalRatio")
    public ResponseEntity<Double> getApprovalRatio() {
        double ratio = productionTrackingService.getApprovedRatio();
        return new ResponseEntity<>(ratio, HttpStatus.OK);
    }
}